package com.example.music.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentContainerView;

import com.example.music.R;
import com.example.music.activity.FavoritesActivity;
import com.example.music.activity.LocalMusicActivity;
import com.example.music.activity.RecentActivity;
import com.example.music.fragment.PlaylistFragment;
import com.example.music.model.AppConstants;

public class MyHome extends Fragment {

    private static final String TAG = "MyHome";
    private PlaylistFragment playlistFragment;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        AppConstants.num = 3;
        View root = inflater.inflate(R.layout.fragment_myhome, container, false);

        // 初始化 PlaylistFragment
        FragmentContainerView fragmentContainer = root.findViewById(R.id.playlist_container);
        if (fragmentContainer != null) {
            Fragment playlistFragment = getChildFragmentManager().findFragmentById(R.id.playlist_container);
            if (playlistFragment == null) {
                Log.d(TAG, "PlaylistFragment not found, adding new instance.");
                getChildFragmentManager().beginTransaction()
                        .add(R.id.playlist_container, new PlaylistFragment())
                        .commit();
            } else {
                Log.d(TAG, "PlaylistFragment already added.");
            }
        } else {
            Log.w(TAG, "FragmentContainerView with id 'playlist_container' not found in layout.");
        }

        // 添加功能区域点击事件
        setupFunctionClicks(root);

        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 刷新 PlaylistFragment
        if (playlistFragment != null) {
            playlistFragment.refreshData();
        }
    }

    private void setupFunctionClicks(View root) {
        View localLayout = root.findViewById(R.id.localLayout);
        View recentPlayLayout = root.findViewById(R.id.recentPlayLayout);
        View downloadLayout = root.findViewById(R.id.downloadLayout);
        View starLayout = root.findViewById(R.id.starLayout);
        View fmLayout = root.findViewById(R.id.fmLayout);
        View boughtLayout = root.findViewById(R.id.boughtLayout);

        if (localLayout != null) {
            localLayout.setOnClickListener(v -> startActivity(new Intent(requireActivity(), LocalMusicActivity.class)));
        }
        if (recentPlayLayout != null) {
            recentPlayLayout.setOnClickListener(v -> startActivity(new Intent(requireActivity(), RecentActivity.class)));
        }
        if (starLayout != null) {
            starLayout.setOnClickListener(v -> startActivity(new Intent(requireActivity(), FavoritesActivity.class)));
        }
        if (downloadLayout != null) {
            downloadLayout.setOnClickListener(v -> showToast("待实现"));
        }
        if (fmLayout != null) {
            fmLayout.setOnClickListener(v -> showToast("待实现"));
        }
        if (boughtLayout != null) {
            boughtLayout.setOnClickListener(v -> showToast("待实现"));
        }
    }

    private void showToast(String message) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
    }
}



